import json

from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np

# Sample data
data = json.load(open("res.json", "r"))

# Extract data for regression
X = np.array([[d["create"], d["other"]] for d in data])
y = np.array([d["vsync"] for d in data])

# Train linear regression model
model = LinearRegression()
model.fit(X, y)

# Print out the parameters of the linear regression model
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)


# Create mesh grid for plotting
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1))

# Predict on mesh grid and apply threshold
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = (Z > 8.3 * 10**6).astype(int)  # Convert to binary
Z = Z.reshape(xx.shape)

# Plot binary decision boundary
plt.contourf(xx, yy, Z, alpha=0.3, cmap="RdYlBu")


model.coef_ = np.array([50000, 20000])
model.intercept_ = np.array(1500000)

print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)

# Create mesh grid for plotting
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1))

# Predict on mesh grid and apply threshold
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = (Z > 8.3 * 10**6).astype(int)  # Convert to binary
Z = Z.reshape(xx.shape)

# Plot binary decision boundary
plt.contourf(xx, yy, Z, alpha=0.3, cmap="RdYlBu")

# Plot data points
create = [d["create"] for d in data]
other = [d["other"] for d in data]
vsync = [d["vsync"] for d in data]

# Color dots based on vsync value
colors = ["green" if v <= 8.3 * 10**6 else "red" for v in vsync]

plt.scatter(create, other, c=colors, s=100, alpha=0.7)

# Add labels and title
plt.xlabel("Create")
plt.ylabel("Other")
plt.title("Linear Regression Decision Boundary")

# Show plot
plt.show()


# Coefficients: [53712.06955112 18833.75842312]
# Intercept: 1366189.7610794036
# Coefficients: [50000 20000]
# Intercept: 1500000
